﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;


namespace IHISCETeim.Web.AgentSys.PWMgr
{
    public partial class PartsQueryList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "PartsQueryList";
            }
        }

        protected int CacheMinutes { get { return 30; } }
        /// <summary>
        /// 验证码缓存KEY
        /// </summary>
        protected string QueryVerKey {
            get {
                return "QueryVerKey" + base.User.UserId.ToString();
            }
        }
        /// <summary>
        /// 验证码
        /// </summary>
        protected string QueryVerCode {
            get
            {
                if (Request.Cookies[QueryVerKey] != null)
                {
                    HttpCookie aCookie = Request.Cookies[QueryVerKey];
                    return StringHelper.Decrypt(aCookie.Value);
                    //Label1.Text = Server.HtmlEncode(aCookie.Value);
                }
                return "";

                //if (Cache[QueryVerKey] == null)
                //{
                //    return "";
                //}
                //return Cache[QueryVerKey].ToString();
            }
            set
            {
                // Cache.Insert(QueryVerKey, value, null, DateTime.UtcNow.AddMinutes(CacheMinutes),System.Web.Caching.Cache.NoSlidingExpiration);  //这里给数据加缓存，设置缓存时间
                //
                Response.Cookies[QueryVerKey].Value = StringHelper.Encrypt(value);
                Response.Cookies[QueryVerKey].Expires = DateTime.Now.AddMinutes(CacheMinutes);
            }
        }
        /// <summary>
        /// 输入的验证码
        /// </summary>
        protected string InputQueryVerCode
        {
            get
            {
                return GetRequestValue<string>("InputQueryVerCode", "");
            }
        }

        protected string VerStr = "";

        protected string ItemName
        {
            get
            {
                return GetRequestValue<string>("ItemName", "");

            }
        }
     
        protected override string orderField
        {
            get
            {
                return GetRequestValue<string>("orderfield", "PWUMKID");
            }
        }

        protected override string orderDirection
        {
            get
            {
                return GetRequestValue<string>("orderDirection", "DESC");
            }
        }


        protected string GetSqlWhere()
        {
            string wheresql = "1<>1";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(ItemName))
            {
                sqlcommonFiled = string.Format(" (oitm.itemcode like '%{0}%' or oitm.itemname like '%{0}%') ",ItemName);
                wheresql = sqlcommonFiled;
            }
           
            return wheresql;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("action", "") == "query")
            {
                DataBind();
            }
            if (GetRequestValue<string>("action", "") == "getvercode")
            {
                GetVerCode();
            }

        }

        protected void GetVerCode()
        {
             string _vercode = StringHelper.CreateRandomCode(4);
            SEMailListController.Add(base.User.EMail, string.Format("本次配件查询验证码为{0}", _vercode), string.Format("注意:此验证码[{1}]只能使用{0}分钟，过期请重新获取", CacheMinutes, _vercode));

            QueryVerCode = _vercode;
        }
      
        protected void DataBind()
        {
            try
            {
                this.rpMenu.DataSource = null;
                this.rpMenu.DataBind();
                if (QueryVerCode == "")
                    return;

                if (QueryVerCode.ToLower().Trim() != "")
                {
                    if (QueryVerCode.ToLower().Trim() != InputQueryVerCode.ToLower().Trim())
                    {
                        VerStr = "请输入正确的验证码.";
                        return;
                    }
                    VerStr = "";
                }


                DataTable dtc = SBOController.GetQueryParts(GetSqlWhere());
                this.rpMenu.DataSource = dtc;
                this.rpMenu.DataBind();
            }
            catch {

            }
        }
       
    }
}